//配置vuex
//引入Vue
import Vue from "vue";
//引入Vuex
import Vuex from 'vuex'
//将请求的API派发给仓库
import { reqUserInfo,reqSearchUser,reqDeleteUser,reqCarInfo,reqSearchCar,
    reqDeleteCar,reqLoginUser,reqLoginCar,reqGarage,reqCreateCar,reqNowCar,reqOrder} from "@/api/index";

//使用插件
Vue.use(Vuex)

//创建并暴露仓库
export default new Vuex.Store({
    //state:仓库、存储数据
    state:{
        // loginAfter:false,//隐藏导航栏
        userList:[],//用户管理信息
        carList:[],//车辆管理信息
        garageList:[],//车位信息
        userId:'',//当前登录用户id
        userDTO:{},//当前登录用户信息
        carDTO:{},//当前登录用户车辆信息
        orderList:[],//订单信息(管理员)
        t:2,//总数
    },

    //action:可以书写逻辑，处理异步请求
    actions:{

        //统计报表-获取所有已完成的订单(管理员
        async getOrder(context,data){
            const result = await reqOrder(data.currentPage,data.pageSize)
            if(result.success){
                context.commit('GETORDER',result.data)
            }
        },
        //获取当前车位情况
        async getGarage(context,data){
            const result = await reqGarage(data.currentPage,data.pageSize)
            if(result.success){
                context.commit('GETGARAGE',result.data)
            }
        },

        //获取用户信息的数据
        async getUserInfo(context,data){
            const result = await reqUserInfo(data.currentPage,data.pageSize)
            if(result.success) {
                context.commit('GETUSERINFO',result.data)
            }
        },
        //搜索框筛选用户数据
        async getUserByName(context,params){
            const result = await reqSearchUser(params.username,params.currentPage)
            if(result.success) {
                context.commit('GETUSERBYNAME',result.data.records)
            }
        },
        //根据id删除用户
        async deleteUser(context,userId){
            await reqDeleteUser(userId)
        },


        //获取车辆信息的数据
        async getCarInfo(context,data){
            const result = await reqCarInfo(data.currentPage,data.pageSize)
            if(result.success) {
                context.commit('GETCARINFO',result.data)
            }
        },
        //搜索框筛选车辆数据
        async getCarByCarNum(context,params){
            const result = await reqSearchCar(params.carNum,params.currentPage)
            if(result.success) {
                context.commit('GETCARBYCARNUM',result.data.records)
            }
        },
        //根据id删除车辆
        async deleteCar(context,carId){
            await reqDeleteCar(carId)
        },

        //获取当前登录用户信息
        async getLoginUser(context,userId){
            const result = await reqLoginUser(userId)
            if(result.success){
                context.commit('GETLOGINUSER',result.data)
            }
        },

        //登录成功后自动创建用户车辆信息(默认)
        async createCar(context,userId){
            const result =  await reqCreateCar(userId)
            if(result.success){
                context.commit('CREATECAR',result.data)
            }
        },
        
        //获取当前登录用户的车辆信息
        async getLoginCar(context,userId){
            const result = await reqLoginCar(userId)
            if(result.success){
                context.commit('GETLOGINCAR',result.data)
            }
        },

        //获取最新的车辆信息
        async getNowCar(context,carId){
            const result = await reqNowCar(carId)
            if(result.success){
                context.commit('GETNOWCAR',result.data)
            }
        },
    },

    //mutations:修改state的数据
    mutations:{
        GETUSERINFO(state,value){
            state.userList = value.records
            state.t = value.total;
        },
        GETUSERBYNAME(state,value){
            state.userList = value
        },
        GETCARINFO(state,value){
            state.carList = value.records
            state.t = value.total
        },
        GETCARBYCARNUM(state,value){
            state.carList = value
        },
        GETLOGINUSER(state,value){
            state.userDTO = value
        },
        CREATECAR(state,value){
            state.carDTO = value
        },
        GETLOGINCAR(state,value){
            state.carDTO = value
        },
        GETGARAGE(state,value){
            state.garageList = value.records
            state.t = value.total;
        },
        GETNOWCAR(state,value){
            state.carDTO = value
        },
        GETORDER(state,value){
            state.orderList = value.records
            state.t = value.total
        }
    },

    //getters:对state数据进行加工(计算属性)
    getters:{
        t:(state)=>{
            return state.t
        },
        isAdmin(){
            return this.userDTO.state
        }
    }
})